package org.dreamwork.jasmine2.javac;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-1-22
 * Time: 10:07:46
 */
public class JasmineFileInfo {
    public static final int TYPE_PAGE = 0;
    public static final int TYPE_USER_CONTROL = 1;

    private String orignalPath;
    private String packageName;
    private String packagePath;
    private String fileName;
    private String className;
    private int type = TYPE_PAGE;

    private File srcdir, outdir;

    private static final char[] ESCAPE = {
            '~', '`', '!', '@', '#', '%', '^', '&', '(', ')', '-', '+', '=', '|', '{', '}', '[', ']', ':', ';', '\'', '\'',
            '<', ',', '>', '?'
    };

    private static final String defaultPackagePath = "jasmine/autogen";

    public JasmineFileInfo (String path, File srcdir, File outdir) {
        this.orignalPath = path;

        if (path.charAt (0) == '/') path = path.substring (1);
        if (path.charAt (0) == File.separatorChar) path = path.substring (1);
        int index = path.lastIndexOf ('/');

        packagePath = defaultPackagePath;
        if (index != -1) {
            packagePath += "/" + path.substring (0, index);
            fileName = path.substring (index + 1);
        } else {
            fileName = path;
        }
        for (char escape : ESCAPE) {
            if (packagePath.indexOf (escape) >= 0)
                packagePath = packagePath.replace (String.valueOf (escape), String.format ("$%02X", (int)escape));
            if (fileName.indexOf (escape) >= 0)
                fileName = fileName.replace (String.valueOf (escape), String.format ("$%02X", (int)escape));
        }
        int pos = fileName.lastIndexOf ('.');
        String ext = fileName.substring (pos + 1);
        fileName = fileName.substring (0, pos);
        fileName = fileName + "_" + ext;

        if ("jscx".equals (ext)) type = TYPE_USER_CONTROL;

        packageName = packagePath.replace ('/', '.');
        className = packageName + "." + fileName;

        this.srcdir = srcdir;
        this.outdir = outdir;
    }

    public void createPackageDir () {
        File packagedir = new File (srcdir, packagePath);
        if (!packagedir.exists ()) packagedir.mkdirs ();        
    }

    public String getPackagePath () {
        return packagePath;
    }

    public String getPackageName () {
        return packageName;
    }

    public String getClassName () {
        return className;
    }

    public String getFileName () {
        return fileName;
    }

    public String getOrignalPath () {
        return orignalPath;
    }

    public int getType () {
        return type;
    }

    public File getSrcFile () {
        return new File (srcdir, className.replace ('.', '/') + ".java");
    }

    public File getClassFile () {
        return new File (outdir, className.replace ('.', '/') + ".class");
    }

    public File getSourceDir () {
        return srcdir;
    }

    public File getClassDir () {
        return outdir;
    }

    @Override
    public String toString () {
        return orignalPath; 
    }
}